package com.cnfangmao.masterdata.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.BasicDataFieldMetadataDao;
import com.cnfangmao.masterdata.dto.BasicDataFieldMetadataSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataFieldMetadata;
import com.cnfangmao.masterdata.service.BasicDataFieldMetadataService;

/**
 * <p>
 * 字段元数据表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class BasicDataFieldMetadataServiceImpl extends ServiceImpl<BasicDataFieldMetadataDao, BasicDataFieldMetadata> implements BasicDataFieldMetadataService {
	
	@Autowired
	private BasicDataFieldMetadataDao dao;
	
	@Override
	public BasicDataFieldMetadata findByFieldMetadataCode(String fieldCode) {
		BasicDataFieldMetadata basicDataFieldMetadata = null;
		QueryWrapper<BasicDataFieldMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("field_code", fieldCode);
        basicDataFieldMetadata = dao.selectOne(queryWrapper);
		return basicDataFieldMetadata;
	}

	@Override
	public BasicDataFieldMetadata findByFieldMetadataName(String fieldName) {
		BasicDataFieldMetadata basicDataFieldMetadata = null;
		QueryWrapper<BasicDataFieldMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("field_name", fieldName);
        basicDataFieldMetadata = dao.selectOne(queryWrapper);
		return basicDataFieldMetadata;
	}

	@Override
	public IPage<BasicDataFieldMetadata> queryListBasicDataFieldMetadata(
			GeneralSearchDto<BasicDataFieldMetadataSearchDto> dto) {
		IPage<BasicDataFieldMetadata> page = dao.selectPage(new Page<>(dto.getPageNo(), dto.getPageSize()),
				new QueryWrapper<BasicDataFieldMetadata>().
				like(StringUtils.isNotEmpty(dto.getConditions().getFieldName()), "field_name", dto.getConditions().getFieldName())
				.or().like(StringUtils.isNotEmpty(dto.getConditions().getFieldCode()), "field_code", dto.getConditions().getFieldCode())
				);

		return page; 
	}

	@Override
	public List<BasicDataFieldMetadata> findByFieldMetadataId(String metadataId) {
		QueryWrapper<BasicDataFieldMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("metadata_id", metadataId);
        List<BasicDataFieldMetadata> basicDataFieldMetadata = dao.selectList(queryWrapper);
		return basicDataFieldMetadata;
	}

	@Override
	public boolean checkField(String userId, String metadataId,
			Map<String, Object> rowData) {
		QueryWrapper<BasicDataFieldMetadata> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("metadata_id", metadataId);
//        List<BasicDataFieldMetadata> basicDataFieldMetadataList = dao.selectList(queryWrapper);
//        for (BasicDataFieldMetadata basicDataFieldMetadata:basicDataFieldMetadataList) {
//			
//		}
		return false;
	}

}
